function switchTab(tab) {
    const loginForm = document.getElementById('loginForm');
    const registerForm = document.getElementById('registerForm');
    const socialLogin = document.querySelector('.social-login');
    const tabs = document.querySelectorAll('.tab-btn');
    const welcomeText = document.querySelector('.welcome-text');

    if (tab === 'login') {
        loginForm.style.display = 'block';
        registerForm.style.display = 'none';
        socialLogin.style.display = 'block';
        tabs[0].classList.add('active');
        tabs[1].classList.remove('active');
        welcomeText.textContent = '欢迎回来';
    } else {
        loginForm.style.display = 'none';
        registerForm.style.display = 'block';
        socialLogin.style.display = 'none';
        tabs[0].classList.remove('active');
        tabs[1].classList.add('active');
        welcomeText.textContent = '创建账号';
    }
}

// 登录表单提交处理
document.getElementById('loginForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const username = document.getElementById('loginUsername').value;
    const password = document.getElementById('loginPassword').value;
    
    // 这里添加登录逻辑
    console.log('登录:', { username, password });
});

// 注册表单提交处理
document.getElementById('registerForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const username = document.getElementById('registerUsername').value;
    const email = document.getElementById('registerEmail').value;
    const password = document.getElementById('registerPassword').value;
    const confirmPassword = document.getElementById('confirmPassword').value;

    if (password !== confirmPassword) {
        alert('两次输入的密码不一致！');
        return;
    }

    // 这里添加注册逻辑
    console.log('注册:', { username, email, password });
});

// 密码强度检查
function checkPasswordStrength(password) {
    let strength = 0;
    if (password.length >= 8) strength++;
    if (/[A-Z]/.test(password)) strength++;
    if (/[0-9]/.test(password)) strength++;
    if (/[^A-Za-z0-9]/.test(password)) strength++;

    const strengthBar = document.querySelector('.strength-bar');
    const strengthText = document.querySelector('.strength-text');

    strengthBar.className = 'strength-bar';
    if (strength >= 4) {
        strengthBar.classList.add('strong');
        strengthText.textContent = '密码强度: 强';
    } else if (strength >= 2) {
        strengthBar.classList.add('medium');
        strengthText.textContent = '密码强度: 中';
    } else {
        strengthBar.classList.add('weak');
        strengthText.textContent = '密码强度: 弱';
    }
}

// 表单验证
document.getElementById('registerPassword').addEventListener('input', function(e) {
    checkPasswordStrength(e.target.value);
});

// 实时验证用户名
document.getElementById('registerUsername').addEventListener('input', function(e) {
    const username = e.target.value;
    const parent = e.target.closest('.input-icon');
    const tip = parent.querySelector('.input-tip');

    if (username.length < 3 || username.length > 20) {
        parent.classList.add('error');
        tip.textContent = '用户名长度必须在3-20个字符之间';
    } else {
        parent.classList.remove('error');
        tip.textContent = '用户名长度为3-20个字符';
    }
});

// 实时验证邮箱
document.getElementById('registerEmail').addEventListener('input', function(e) {
    const email = e.target.value;
    const parent = e.target.closest('.input-icon');
    const tip = parent.querySelector('.input-tip');
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    if (!emailRegex.test(email)) {
        parent.classList.add('error');
        tip.textContent = '请输入有效的邮箱地址';
    } else {
        parent.classList.remove('error');
        tip.textContent = '邮箱格式正确';
    }
});

// 提交时的加载状态
function showLoading(button) {
    button.classList.add('loading');
    button.disabled = true;
}

function hideLoading(button) {
    button.classList.remove('loading');
    button.disabled = false;
}

// 更新表单提交处理
document.getElementById('loginForm').addEventListener('submit', async function(e) {
    e.preventDefault();
    const submitBtn = this.querySelector('.submit-btn');
    showLoading(submitBtn);

    try {
        // 模拟API请求
        await new Promise(resolve => setTimeout(resolve, 1500));
        console.log('登录成功');
    } catch (error) {
        console.error('登录失败:', error);
    } finally {
        hideLoading(submitBtn);
    }
});

// 添加找回密码相关函数
function showResetPasswordForm() {
    const loginForm = document.getElementById('loginForm');
    const registerForm = document.getElementById('registerForm');
    const resetPasswordForm = document.getElementById('resetPasswordForm');
    const socialLogin = document.querySelector('.social-login');
    const tabs = document.querySelector('.tabs');

    // 隐藏其他表单和元素
    loginForm.style.display = 'none';
    registerForm.style.display = 'none';
    socialLogin.style.display = 'none';
    tabs.style.display = 'none';

    // 显示找回密码表单
    resetPasswordForm.style.display = 'block';
}

function hideResetPasswordForm() {
    const loginForm = document.getElementById('loginForm');
    const resetPasswordForm = document.getElementById('resetPasswordForm');
    const socialLogin = document.querySelector('.social-login');
    const tabs = document.querySelector('.tabs');

    // 添加淡出动画
    resetPasswordForm.classList.add('fade-out');

    // 等待动画完成后切换显示
    setTimeout(() => {
        resetPasswordForm.style.display = 'none';
        resetPasswordForm.classList.remove('fade-out');
        
        // 显示登录表单和相关元素
        loginForm.style.display = 'block';
        socialLogin.style.display = 'block';
        tabs.style.display = 'flex';
    }, 300);
}

// 发送验证码
function sendVerifyCode() {
    const email = document.getElementById('resetEmail').value;
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    
    if (!emailRegex.test(email)) {
        alert('请输入有效的邮箱地址');
        return;
    }

    const btn = document.querySelector('.send-code-btn');
    let countdown = 60;
    
    btn.disabled = true;
    btn.textContent = `${countdown}秒后重试`;

    const timer = setInterval(() => {
        countdown--;
        if (countdown === 0) {
            clearInterval(timer);
            btn.disabled = false;
            btn.textContent = '获取验证码';
        } else {
            btn.textContent = `${countdown}秒后重试`;
        }
    }, 1000);

    // 模拟发送验证码
    console.log('发送验证码到邮箱:', email);
}

// 修改找回密码表单提交处理
document.getElementById('resetPasswordForm').addEventListener('submit', async function(e) {
    e.preventDefault();
    const submitBtn = this.querySelector('.submit-btn');
    const email = document.getElementById('resetEmail').value;
    const verifyCode = document.getElementById('verifyCode').value;
    const newPassword = document.getElementById('newPassword').value;
    const confirmNewPassword = document.getElementById('confirmNewPassword').value;
    
    if (!verifyCode) {
        alert('请输入验证码');
        return;
    }

    if (newPassword !== confirmNewPassword) {
        alert('两次输入的密码不一致！');
        return;
    }

    showLoading(submitBtn);

    try {
        // 模拟API请求
        await new Promise(resolve => setTimeout(resolve, 1500));
        alert('密码重置成功！');
        hideResetPasswordForm();
    } catch (error) {
        console.error('重置失败:', error);
        alert('重置失败，请稍后重试');
    } finally {
        hideLoading(submitBtn);
    }
});

// 添加新密码强度检查
document.getElementById('newPassword').addEventListener('input', function(e) {
    checkPasswordStrength(e.target.value);
});

// 添加事件监听器
document.addEventListener('DOMContentLoaded', function() {
    // 忘记密码链接点击事件
    document.querySelector('.forgot-password').addEventListener('click', function(e) {
        e.preventDefault();
        showResetPasswordForm();
    });

    // 返回登录链接点击事件
    document.querySelector('.back-to-login').addEventListener('click', function(e) {
        e.preventDefault();
        hideResetPasswordForm();
    });
}); 